//
//  RTJUserCenterController.m
//  RuiTaiJinProject
//
//  Created by jayson on 2018/11/26.
//  Copyright © 2018年 leewei. All rights reserved.
//

#import "RTJUserCenterController.h"
#import "RTJUserCenterCell.h"
#import "RTJLoginOrRegisterController.h"
#import "RTJUserInfoController.h"
#import "RTJSettingController.h"
#import "RTJChargeController.h"
#import "RTJForgetPassWordController.h"
#import "RTJBidRecordsRootController.h"
#import "RTJChargeRootController.h"
#import "RTJExperienceGoldController.h"
#import "RTJInvitationController.h"
#import "RTJCapitalSubsidiaryController.h"
#import "RTJAutomaticBidDetailsController.h"
#import "RTJAutomaticBidController.h"
#import "RTJWithdrawalController.h"
#import "RTJOpinionController.h"
#import "RTJBindPayCardController.h"
#import "RTJMymanagerController.h"
#import "RTJMyClientController.h"
#import "RTJNoticeView.h"
#import "LewPopupViewAnimationFade.h"
#import "RTJDebtCalendarController.h"
#import "RTJReimbursementController.h"

@interface RTJUserCenterController ()<ButtonActionDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,assign) BOOL isSelected;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tabTop;

@end

@implementation RTJUserCenterController

-(void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBarHidden = YES;
    
    [self.tableView reloadData];

    
    if ([RTJUserTool isLogin]) {

        [RTJNetWork asyncRequestWithURL:@"user/get_user_info" params:@{@"id":[RTJUserTool getTheUserModel].user_id} method:POST zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {

            if (success) {
                
                if ([data[@"status"]integerValue] == 1) {
                    
                    [RTJUserTool saveTheUserInfo:data[@"data"]];
                    
                    [self.tableView reloadData];
                    
                }else{
                    
                    [RTJUserTool loginOut];
                    [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",data[@"msg"]]];
                    
                    [self.tableView reloadData];
                    
                }
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",errorMsg]];

            }
            
        }];
        
    }else{
        
        [RTJUserTool loginOut];
        [self.tableView reloadData];

    }
}

-(void)viewDidDisappear:(BOOL)animated{
    
    self.navigationController.navigationBarHidden = NO;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.tableFooterView=[UIView new];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.estimatedRowHeight=300;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tabTop.constant = screenH == 812 ? -20:0;
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 5;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return UITableViewAutomaticDimension;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * ID = @"RTJUserCenterCell";
    
    RTJUserCenterCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (indexPath.row == 0) {
        
        if (!cell) {
            
            cell = [[NSBundle mainBundle] loadNibNamed:@"RTJUserCenterCell" owner:self options:nil][0];
        }
        
    }else if (indexPath.row == 1){
        
        if (!cell) {
            
            if ([RTJUserTool isLogin]) {
                cell = [[NSBundle mainBundle] loadNibNamed:@"RTJUserCenterCell" owner:self options:nil][2];
                
                if (_isSelected) {
                    
                    cell.totalMoney.text = @"******";
                    cell.xiaoshudian.text = @"元";
                    cell.daishouyi.text = @"待收金额:******元";
                    cell.leijishouyi.text = @"冻结金额:******元";
                    cell.btn3.selected = _isSelected;
                    
                }else{
                    
                    cell.totalMoney.text = [RTJUserTool isLogin]==YES ? [NSString stringWithFormat:@"%@",[RTJUserTool getTheUserModel].total_money_format] :@"0";
                    cell.xiaoshudian.text = @"元";
                    cell.daishouyi.text = [RTJUserTool isLogin]==YES ? [NSString stringWithFormat:@"待收金额:%@元",[RTJUserTool getTheUserModel].daishou_format] :@"待收金额:0.00元";
                    cell.leijishouyi.text = [RTJUserTool isLogin]==YES ? [NSString stringWithFormat:@"冻结金额:%@元",[RTJUserTool getTheUserModel].lock_money_format] :@"冻结金额:0.00元";
                    cell.btn3.selected = _isSelected;

                }

            }else{
                
                cell = [[NSBundle mainBundle] loadNibNamed:@"RTJUserCenterCell" owner:self options:nil][1];

            }
            
        }
        
    }else if (indexPath.row == 2){
        
        if (!cell) {
            
            cell = [[NSBundle mainBundle] loadNibNamed:@"RTJUserCenterCell" owner:self options:nil][3];
            
        }
        
        if (_isSelected) {
            
            cell.shengyu.text = [RTJUserTool isLogin] == YES ? [NSString stringWithFormat:@"******元"] :@"0.00元";
            cell.shouyiLab.text = [RTJUserTool isLogin] == YES ? [NSString stringWithFormat:@"******元"] :@"0.00元";

        }else{
            
            cell.shengyu.text = [RTJUserTool isLogin]==YES ? [NSString stringWithFormat:@"%@元",[RTJUserTool getTheUserModel].money_format] :@"0.00元";
            cell.shouyiLab.text = [RTJUserTool isLogin]==YES ? [NSString stringWithFormat:@"%@元",[RTJUserTool getTheUserModel].calculate_daily_earnings] :@"0.00元";

        }
        
    }else if (indexPath.row == 3){
        
        if (!cell) {
            
            cell = [[NSBundle mainBundle] loadNibNamed:@"RTJUserCenterCell" owner:self options:nil][4];
            
        }
        
        if (![RTJUserTool isLogin]|| [RTJUserTool getTheUserModel].group_id != 2) {
            
            cell.dotTop.constant = 0;
            cell.dotHeight.constant = 0;
            cell.dotBottom.constant = 0;
            cell.guanliLab.hidden = YES;
            cell.lineHeight.constant = 0;
            cell.btn18.hidden = YES;
            cell.bg4Height.constant = 331-18-23-12;
            
        }
        
        
    }else{
        
        if (!cell) {
            
            cell = [[NSBundle mainBundle] loadNibNamed:@"RTJUserCenterCell" owner:self options:nil][5];
            
        }
        
        if (![RTJUserTool isLogin]|| [RTJUserTool getTheUserModel].type == 3) {
          
            cell.labHeight1.constant = 0;
            cell.view5.hidden = YES;
            cell.imgTop1.constant = 0;
            cell.imgBottom.constant = 0;
            cell.btn15.hidden = YES;
            cell.img5H.constant = 0;
            cell.imgView.hidden = YES;
            cell.bg_view5Height.constant = 165-18-23-12;
            
        }else{
            
            if ([RTJUserTool getTheUserModel].type == 1) {
                
                if ([RTJUserTool getTheUserModel].admin_id == 0) {
                   
                    cell.labHeight1.constant = 0;
                    cell.view5.hidden = YES;
                    cell.imgTop1.constant = 0;
                    cell.imgBottom.constant = 0;
                    cell.btn15.hidden = YES;
                    cell.imgView.hidden = YES;
                    cell.img5H.constant = 0;
                    cell.bg_view5Height.constant = 165-18-23-12;
                    
                }else{
                    
                    cell.labHeight1.constant = 15;
                    cell.view5.hidden = NO;
                    cell.imgTop1.constant = 23;
                    cell.imgBottom.constant = 18;
                    cell.btn15.hidden = NO;
                    cell.imgView.hidden = NO;
                    cell.bg_view5Height.constant = 165;
                    cell.kehuLab.text = @"客户经理";
                    
                }
                
            }else{
                
                cell.labHeight1.constant = 15;
                cell.view5.hidden = NO;
                cell.imgTop1.constant = 23;
                cell.imgBottom.constant = 18;
                cell.btn15.hidden = NO;
                cell.imgView.hidden = NO;
                cell.bg_view5Height.constant = 165;
                cell.kehuLab.text = @"我的客户";
                
            }
        }
    }
    cell.delegate = self;
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return  cell;
    
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//
//}

-(void)buttonOfAction:(BOOL )isSelected{
    
    _isSelected = isSelected;
    
//    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:2 inSection:0];
//    NSIndexPath *indexPath1=[NSIndexPath indexPathForRow:3 inSection:0];
//    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,indexPath1,nil] withRowAnimation:UITableViewRowAnimationNone];
    [self.tableView reloadData];
  
    
}

-(void)buttonOfTagsAction:(NSInteger)buttonOfTag{
    
    if (buttonOfTag == 130){
        
        //帮助中心
        RTJOpinionController *vc = [[RTJOpinionController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
        
        
    }else if (buttonOfTag == 120){
        //客服
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"拨打400-686-8063客服电话" message:@"服务时间:周一至周五9:00-17:30" preferredStyle:UIAlertControllerStyleAlert];
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", @"400-686-8063"]];
            [[UIApplication sharedApplication] openURL:url];
            
        }]];
        
        [self presentViewController:alertController animated:YES completion:nil];
        
        
    }else if (buttonOfTag == 300){
        
        RTJNoticeView *view = [RTJNoticeView defaultViewWithFrame:CGRectMake(0, 0, 325, 197)];
        view.parentVC = self;
        [self lew_presentPopupView:view animation:[LewPopupViewAnimationFade new] dismissed:^{
            
            NSLog(@"动画结束");
            
        }];
        
        [view withSureClick:^(void) {
            
        }];
        
        
    }else{
        
        if ([RTJUserTool isLogin]) {
            
            if (buttonOfTag == 5) {
                
                //账户明细
                RTJCapitalSubsidiaryController *vc =  [[RTJCapitalSubsidiaryController alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
                
            }else if (buttonOfTag == 10) {
                
                RTJUserInfoController *VC = [[RTJUserInfoController alloc]init];
                [self.navigationController pushViewController:VC animated:YES];
                
            }else if (buttonOfTag == 20){
                
                RTJSettingController *VC = [[RTJSettingController alloc]init];
                [self.navigationController pushViewController:VC animated:YES];
                
            }else if (buttonOfTag == 30){
                
                RTJLoginOrRegisterController *vc = [[RTJLoginOrRegisterController alloc]init];
                LXNavigationViewController *nav=[[LXNavigationViewController alloc]initWithRootViewController:vc];
                [self presentViewController:nav animated:YES completion:nil];
                
            }else if (buttonOfTag == 40){
                
            }else if (buttonOfTag == 50){
                
                
                if ([RTJUserTool getTheUserModel].user_bank.count == 0 ||[RTJUserTool getTheUserModel].user_bank==nil) {
                    
                    RTJBindPayCardController *vc = [[RTJBindPayCardController alloc]init];
                    [self.navigationController pushViewController:vc animated:YES];
                    
                }else{
                    
                    //提现
                    //是否已设置支付密码
                    if ([RTJUserTool getTheUserModel].paypassword.length ==0) {
                        
                        [SVProgressHUD showErrorWithStatus:@"你还没有设置支付密码"];
                        
                        RTJForgetPassWordController *vc = [[RTJForgetPassWordController alloc]init];
                        vc.isPayPassWord = YES;
                        vc.title = @"设置支付密码";
                        [self.navigationController pushViewController:vc animated:YES];
                        
                    }else{
                        
                        RTJWithdrawalController *VC = [[RTJWithdrawalController alloc]init];
                        [self.navigationController pushViewController:VC animated:YES];
                        
                    }
                }
                
            }else if (buttonOfTag == 60){
                
                //充值
                
                RTJChargeController *VC = [[RTJChargeController alloc]init];
                [self.navigationController pushViewController:VC animated:YES];
                
            }else if (buttonOfTag == 70){
                
                //出借
                RTJBidRecordsRootController *VC =  [[RTJBidRecordsRootController alloc]init];
                [self.navigationController pushViewController:VC animated:YES];
                
            }else if (buttonOfTag == 80){
                
                //回款日历
                RTJDebtCalendarController *vc =  [[RTJDebtCalendarController alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
                
            }else if (buttonOfTag == 90){
                //卡券包
                RTJChargeRootController *vc = [[RTJChargeRootController alloc]init];
                vc.isUserCenter = YES;
                [self.navigationController pushViewController:vc animated:YES];
                
            }else if (buttonOfTag == 100){
                
                //体验金
                if ([RTJUserTool getTheUserModel].has_learn == 1) {
                    
                    RTJExperienceGoldController *vc = [[RTJExperienceGoldController alloc]init];
                    [self.navigationController pushViewController:vc animated:YES];
                    
                }else{
                    
                    [SVProgressHUD showErrorWithStatus:@"您暂时没有体验金"];
                }
                
                
            }else if (buttonOfTag == 110){
                
                //邀请
                RTJInvitationController *vc = [[RTJInvitationController alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
                
            }else if (buttonOfTag == 220){
                
                //借款管理
                RTJReimbursementController *vc = [[RTJReimbursementController alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
                
            }else if (buttonOfTag == 200){
                
                if ([RTJUserTool getTheUserModel].type == 1) {
                    
                    RTJMymanagerController *vc = [[RTJMymanagerController alloc]init];
                    vc.title = @"客户经理";
                    [self.navigationController pushViewController:vc animated:YES];
                    
                }else{
                    
                    RTJMyClientController *vc = [[RTJMyClientController alloc]init];
                    vc.title = @"我的客户";
                    [self.navigationController pushViewController:vc animated:YES];
                    
                }
                
            }else{
                
                //自动标
                if ([RTJUserTool getTheUserModel].user_autobid == 1) {
                    
                    RTJAutomaticBidDetailsController *VC = [[RTJAutomaticBidDetailsController alloc]init];
                    [self.navigationController pushViewController:VC animated:YES];
                    
                }else{
                    
                    RTJAutomaticBidController *VC = [[RTJAutomaticBidController alloc]init];
                    [self.navigationController pushViewController:VC animated:YES];
                }
            }
            
        }else{
            
            RTJLoginOrRegisterController *vc = [[RTJLoginOrRegisterController alloc]init];
            LXNavigationViewController *nav=[[LXNavigationViewController alloc]initWithRootViewController:vc];
            [self presentViewController:nav animated:YES completion:nil];
            
        }
        
    }
    
}


-(void)btnClickOfTag:(UIButton *)btn{
    
    

}



@end
